

//
//  CaptureButton.m
//  录音Test
//
//  Created by GANGAI on 2018/2/27.
//  Copyright © 2018年 GANGAI. All rights reserved.
//

#import "CaptureButton.h"

@class GJAnimDelegate;
@protocol GJAnimDelegateDelegate <NSObject>
@optional
- (void)animationDidStop;
@end

/**
 动画代理, 打破CaptureButton执行动画的循环引用,解决CAAnimationGroup的CAAnimationDelegate属性是strong类型导致的内存泄漏
 */
@interface GJAnimDelegate : NSObject <CAAnimationDelegate>
@property(nonatomic, assign) id<GJAnimDelegateDelegate> delegate;
@end

@implementation GJAnimDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if ([self.delegate respondsToSelector:@selector(animationDidStop)]) {
        [self.delegate animationDidStop];
    }
}
@end

/**
 自定义的动画CALayer:为了监控内存释放
 */
@interface CustomCALayer : CALayer

@end

@implementation CustomCALayer
- (void)dealloc {
    if (kDebugViewRelease) {
#ifdef DEBUG
        DBLog(@"\n ----------- CALayer %@ 销毁了 ----------- \n",[self class]);
#endif
    }
}
@end


@interface CaptureButton () <GJAnimDelegateDelegate>
@property (nonatomic, strong) CustomCALayer *circleLayer;
@end

@implementation CaptureButton

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (!self.circleLayer) {
        self.circleLayer = [CustomCALayer new];
        self.circleLayer.backgroundColor = UIColorFromHex(0xFF9A7A).CGColor; // RGB(213, 31, 60)
        self.backgroundColor = [UIColor clearColor];
        self.tintColor = [UIColor clearColor];
        self.circleLayer.frame = CGRectInset(self.bounds, 8, 8); // CGRectInset(self.bounds, 8, 8);
        self.circleLayer.cornerRadius = self.circleLayer.frame.size.width * 0.5;
        [self.layer addSublayer:self.circleLayer];
    }
    
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 设置圆环
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);  // UIColorFromRGB(0x0383e0)  RGB(235, 197, 109)
    // 设置线宽,即圆环宽度
    CGContextSetLineWidth(context, 5); // CGContextSetLineWidth(context, 5);
    // 设置圆环大小
    CGRect circleRect = CGRectInset(rect, 3, 3);
    CGContextStrokeEllipseInRect(context, circleRect);
    
}
/*
-(void)setHighlighted:(BOOL)highlighted {
    
    CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    fadeAnimation.duration = 0.2;
    if (highlighted) {
        fadeAnimation.toValue = @(0);
    }else {
        fadeAnimation.toValue = @(1);
    }
    self.circleLayer.opaque = [fadeAnimation.toValue intValue] == 0 ? NO : YES;
    [self.circleLayer addAnimation:fadeAnimation forKey:@"fadeAnimation"];
    
}
*/
-(void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    self.enabled = NO;
    if (!self.isVideoCamera) {
        return;
    }
    CABasicAnimation *scaleAnimation = [CABasicAnimation animation];
    scaleAnimation.keyPath = @"transform.scale";

    CABasicAnimation *radiusAnimation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];

    if (selected) {
        scaleAnimation.toValue = @(0.5);
        radiusAnimation.toValue = @(self.circleLayer.bounds.size.width/8.0);
    }else{
        scaleAnimation.toValue = @(1.0);
        radiusAnimation.toValue = @(self.circleLayer.bounds.size.width/2.0);
    }
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = @[radiusAnimation,scaleAnimation];
    animationGroup.beginTime = CACurrentMediaTime() + 0.2;
    animationGroup.duration = 0.35;

    /// Bug: 坑爹CAAnimationGroup的代理是strong的,不要使用该方式设置代理
//    animationGroup.delegate = self;
    /// Bug Fixed: 修复该类执行动画的时候内存泄漏的问题 见: https://www.cnblogs.com/chjsun/p/6180321.html
    GJAnimDelegate *animDelegate = [[GJAnimDelegate alloc] init];
    animDelegate.delegate = self;
    animationGroup.delegate = animDelegate;

    // 始终保持最新的效果
    animationGroup.fillMode = kCAFillModeForwards;
    animationGroup.removedOnCompletion = NO;
    [self.circleLayer addAnimation:animationGroup forKey:nil];

}

#pragma mark - GJAnimDelegateDelegate
- (void)animationDidStop {
    self.enabled = YES;
    if (self.animationResultBlock) {
        self.animationResultBlock(self.isSelected);
    }
}

//#pragma mark - CAAnimationDelegate
///* Called when the animation begins its active duration. */
//- (void)animationDidStart:(CAAnimation *)anim {
////    NSLog(@"动画开始: %@ -------- %d",anim,self.isSelected);
//}

/* Called when the animation either completes its active duration or
 * is removed from the object it is attached to (i.e. the layer). 'flag'
 * is true if the animation reached the end of its active duration
 * without being removed. */

//- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
//    self.enabled = YES;
//    if (self.animationResultBlock) {
//        self.animationResultBlock(self.isSelected);
//    }
//}
@end
